import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        int[] arr = {3,2,20,1,1,3};
        String s = "baa";
        String p = "aa";
        findAnagrams(s,p);
    }
    //1658.将x减到0的最小操作数
    public static int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int i =0;i<nums.length;i++) {
            sum+=nums[i];
        }
        int target = sum - x,len=-1,n=nums.length,tmp=0;
        for(int left=0,right=0;right<n;right++) {
            //1.进窗口
            tmp=tmp+nums[right];
            //2.(判断)target==sum的时候不进去
            while(left<n&&tmp > target) {
                tmp = tmp - nums[left];
                left++;
            }
            //3.出窗口
            if(target == tmp) {
                len = Math.max(len,right-left+1);
            }
        }
        return len==-1?-1:n-len;
    }
    //438.找到字符串中所有字母异位词
    public static List<Integer> findAnagrams(String s, String p) {
        int sh = s.length();
        int ph = p.length();
        List<Integer> list = new ArrayList<>();
        int[] map1 = new int[27];
        for(int i=0;i<ph;i++) {
            int index = p.charAt(i);
            map1[index-97]++;
        }
        int[] map = new int[27];
        for(int left = 0,right=0;right<sh;right++) {
            //1.进窗口
            map[s.charAt(right)-97]++;
            //2.判断
            if(right-left+1>ph) {
                map[s.charAt(left)-97]--;
                left++;
            }
            //判断是否为异位词
            if(right-left+1==ph) {
                int count = 0;
                if(count!=0) {
                    break;
                }
                for(int i =0;i<26;i++) {
                    if(map[i] >=1 && map[i] != map1[i]) {
                        count=1;
                        break;
                    }
                }
                //更新结果
                if(count == 0) {
                    list.add(left);
                }
            }
        }
        return list;
    }
}
